﻿var base = {
    init: function() {

        var that = this;
        //$( "#datepicker" ).html().removeClass('hasDatepicker');
        company_id = 0;
        $.ajax({
            url: COMMONURL + 'api/company/getcompanyinfo',
            type: 'get',
            data: {
                token: $.cookie('token')
            },
            dataType: 'json',
            beforeSend: function() {
                $('.jload').show();
            },
            complete: function() {
                $('.jload').hide();
            }
        }).done(function(data) {
        
            var value = data.mod;
            if (!!value) {
                var cityIdArray = value.city_id_x ? value.city_id_x.split('|') : [];
                var area = cityIdArray[0] == undefined ? '' : cityIdArray[0];
                var city = cityIdArray[1] == undefined ? '' : cityIdArray[1];
                var town = cityIdArray[2] == undefined ? '' : cityIdArray[2];
                company_id = data.mod.company_id;
                var compiled = _.template($("#Base_cont").html());
                $("#BaseView").html(compiled(value));
                that.getAddress({
                    type: 10
                }, $("#Jarea"), function () {
                    if (area != '') {
                        $("#Jarea option").each(function (i, t) {
                            if ($(t).attr('data-id') == area) {

                                $(t).attr('selected', true);
                            }
                        })
                    }

                    that.getAddress({
                        type: 20,
                        sub_city_id: area
                    }, $("#Jcity"), function () {
                        if (city != '') {
                            $("#Jcity option").each(function (i, t) {
                                if ($(t).attr('data-id') == city) {
                                    $(t).attr('selected', true);
                                }
                            })
                        }

                        that.getAddress({
                            type: 30,
                            sub_city_id: city
                        }, $("#Jtown"), function () {
                            if (town != '') {
                                $("#Jtown option").each(function (i, t) {
                                    if ($(t).attr('data-id') == town) {
                                        $(t).attr('selected', true);
                                    }
                                })
                            }

                        });
                    });
                });
            }
            else
            {//没有能获取到企业信息
                var compiled = _.template($("#Base_cont").html());
                $("#BaseView").html(compiled(value));
            }
            //that.getAddress({type:20,sub_city_id:area},$("#Jcity"));
            //that.getAddress({type:30,sub_city_id:city},$("#Jtown"));
            that.didaction();
        })
    },
    getAddress: function(type, obj, callback) {
        $.ajax({
            url: COMMONURL + 'api/CityBase/GetCityBaseList?token=' + $.cookie('token'),
            type: 'post',
            dataType: 'json',
            data: type

        }).done(function(data) {
            var arry = [];
            if (!!data.mod) {
                if (!data.mod.length) {
                    arry.push('<option value="' + '" data-id="' + '">' + '请选择' + '</option>');
                } else {
                    $.each(data.mod, function(i, v) {
                        arry.push('<option value="' + v.city_name + '" data-id="' + v.city_id + '">' + v.city_name + '</option>');
                    });
                }
                obj.html(arry.join(''));
                if (typeof callback == 'function') {
                    callback();
                }
            }

        })
    },
    getCity: function() {
        $.ajax({
            url: COMMONURL + 'api/CityBase/GetCityBaseList',
            type: 'post',
            dataType: 'json',
            data: {
                token: $.cookie('token'),
                type: 10
            }

        }).done(function(data) {
            alert(data)
        })
    },
    getTown: function() {
        $.ajax({
            url: COMMONURL + 'api/CityBase/GetCityBaseList',
            type: 'post',
            dataType: 'json',
            data: {
                token: $.cookie('token'),
                type: 10
            }

        }).done(function(data) {
            alert(data)
        })
    },
    didaction: function() {
        var that = this;
        $("#Jarea").change(function() {
            var $this = $(this);
            that.getAddress({
                type: 20,
                sub_city_id: $this.find(':selected').attr('data-id')
            }, $("#Jcity"), function() {
                that.getAddress({
                    type: 30,
                    sub_city_id: $("#Jcity option").eq(0).attr('data-id')
                }, $("#Jtown"));
            });
        })

        $("#Jcity").change(function() {
            var $this = $(this);
            that.getAddress({
                type: 30,
                sub_city_id: $this.find(':selected').attr('data-id')
            }, $("#Jtown"));
        })

        $.ajax({
            url: 'previewimg.js'
        }).done(function(data) {}); //load js



        /*$('#datepicker').datetimepicker({
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0,
		format: 'yyyy-mm-dd'
    });*/
        jQuery.validator.addMethod("isMobile", function(value, element) {
            var length = value.length;
            var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
            return this.optional(element) || (length == 11 && mobile.test(value));
        }, "填写正确的手机号码");

        $("#baseForm").validate({
            rules: {
                account_no: "required",
                company_name: 'required',
                address: 'required',
                contact: 'required',
                phone: {
                    minlength: 11,
                    isMobile: true
                }
            },
            messages: {
                account_no: "请输入用户名",
                company_name: "请输入企业名称",
                address: '请输入详细地址',
                contact: '请输入联系人',
                phone: {

                    minlength: "请确认手机不能小于11个字符",
                    isMobile: "填写正确的手机号码"
                },
            },
            submitHandler: function(form) {
    //alert($("#Jarea").find(":selected").attr('data-id') + '|' + $("#Jcity").find(":selected").attr('data-id') + '|' + $("#Jtown").find(":selected").attr('data-id'));

                $.ajax({
                    url: COMMONURL + "api/company/Modify?token=" + $.cookie('token'),
                    type: "post",
                    dataType: "json",
                    data: {
                        company_id: company_id,
                        company_no: $("#account_no").val(),
                        company_name: $("#company_name").val(),
                        city_name: $("#Jarea").val()+$("#Jcity").val()+$("#Jtown").val(),
                        city_id:$("#Jtown").find(":selected").attr('data-id'),
                        city_id_x: $("#Jarea").find(":selected").attr('data-id') + '|' + $("#Jcity").find(":selected").attr('data-id') + '|' + $("#Jtown").find(":selected").attr('data-id'),
                        address: $("#address").val(),
                        contacts: $("#contact").val(),
                        cont_phone: $("#phone").val(),
                        cont_tel: $("#tel-c").val() + '-' + $("#tel-d").val(),
                        information: $("#com-des").val(),
                        bid_sel: $(".bid_sel:checked").attr('data-id'),
                        price_mode: $(".price_mode:checked").attr('data-id'),
                        empty_price: $("#Emptyamount").val(),
                        nopickup_price: $("#nopick").val(),
                        address_display: $(".address_display:checked").attr('data-id'),
                        fk_info: $("#cwaybill").val(),
                        balance: $("#payedtype").find(':selected').attr('data-id'),
                        fk: $("#paytype").find(':selected').attr('data-id'),
                        bid_minnum:$("#bid_minnum").val(),
                        choice_time:$("#choice_time").val()
                    }
                }).done(function(data) {
                    if (data.recode == 0000) {
                        alert("修改成功！");

                    } else {
                        alert(data.resmessage);
                    }
                })
            }
        })

    }
}

base.init();
